package com.uk.ui.tarifa;

import com.uk.data.containers.TarifaContainer;
import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.StatusEnum;
import com.uk.ui.CHorizontalSplitPanel;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;


public class TarifaPanel extends CHorizontalSplitPanel implements ItemClickListener, ValueChangeListener{

	private IFaturaBean faturaBean;
	
	private TarifaNavigationTree tarifaNavigationTree = new TarifaNavigationTree(this);
	private TarifaContainer dataSource;
	
	private TarifaListView tarifaListView = null;
	private TarifaList tarifaList = null;
	private TarifaForm tarifaForm = null;
	
	public TarifaPanel( IFaturaBean faturaBean){
		

		this.faturaBean = faturaBean;
		this.dataSource = TarifaContainer.createFromEjb(this.faturaBean, StatusEnum.AKTIV);
		this.setSizeFull();

		this.setFirstComponent(this.tarifaNavigationTree);
		setSecondComponent(this.getTarifaListView());
		
		
	}
	
	
	public TarifaListView getTarifaListView(){
		if (this.tarifaListView == null){
			this.tarifaList = new TarifaList(this);
			this.tarifaForm = new TarifaForm(this);
			this.tarifaListView = new TarifaListView(this.tarifaList, this.tarifaForm);
		}
		else {
			this.tarifaList = new TarifaList(this);
			this.tarifaListView.setTarifaList(this.tarifaList);
			
		}
		return tarifaListView;
		
	}
	
	
	@Override
	public void itemClick(ItemClickEvent event) {
		if (event.getSource() == this.tarifaNavigationTree){
			Object itemId = event.getItemId();
			if (itemId != null){
				if (TarifaNavigationTree.LIST.equals(itemId)){
					tarifaNavigationTree.expandItem(TarifaNavigationTree.LIST);	
				}
				else if (TarifaNavigationTree.AKTIVE.equals(itemId)){
					this.dataSource = TarifaContainer.createFromEjb(faturaBean, StatusEnum.AKTIV);
					setSecondComponent(this.getTarifaListView());
					this.tarifaForm.setItemDataSource(null);
				}
				
				else if (TarifaNavigationTree.JOAKTIVE.equals(itemId)){
					this.dataSource = TarifaContainer.createFromEjb(faturaBean, StatusEnum.JOAKTIVE);
					setSecondComponent(this.getTarifaListView());
					this.tarifaForm.setItemDataSource(null);
				}
				else if (TarifaNavigationTree.ADD.equals(itemId)){
					this.tarifaForm.addTarife();
				}
			}
		}
		
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
		if (property == tarifaList){
			Item item = tarifaList.getItem(tarifaList.getValue());
			
			if (item != tarifaForm.getItemDataSource()){
				tarifaForm.setItemDataSource(item);
			}
		}
		
	}
	
	public IFaturaBean getFaturaBean() {
		return faturaBean;
	}


	public TarifaContainer getDataSource() {
		return dataSource;
	}

}
